package com.example.interview.agent.JDK;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-04-07
 * Time:10:18
 * Text：代理实现的类
 */
public class ProxyTest {

    public void m1() {
        IService serviceA = new ServiceA();
        IService serviceB = new ServiceB();
        serviceA.m1();
        serviceA.m2();
        serviceA.m3();
        System.out.println("-----------------------");
        serviceB.m1();
        serviceB.m2();
        serviceB.m3();
    }
    public void m2(){
        IService serviceA = new ServiceProxy(new ServiceA());
        IService serviceB = new ServiceProxy(new ServiceB());

        serviceA.m1();
        serviceA.m2();
        serviceA.m3();
        System.out.println("-----------------------");
        serviceB.m1();
        serviceB.m2();
        serviceB.m3();
    }

//通用代理二
    public  void m3(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 创建 ServiceA 实例
        ServiceA serviceA = new ServiceA();

        // 创建 InvocationHandler 实例
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                try {
                    // 使用反射调用 ServiceA 类的对应方法
                    Method serviceAMethod = ServiceA.class.getMethod(method.getName(), method.getParameterTypes());
                    return serviceAMethod.invoke(serviceA, args);
                } catch (NoSuchMethodException e) {
                    System.out.println("ServiceA 类中没有对应的方法：" + method.getName());
                    // 可以执行一些默认逻辑或者抛出异常
                    return null;
                }
            }
        };

        // 获取接口对应的代理类
        Class<IService> proxyClass = (Class<IService>) Proxy.getProxyClass(IService.class.getClassLoader(), IService.class);

        // 创建代理实例
        IService proxyService = proxyClass.getConstructor(InvocationHandler.class).newInstance(invocationHandler);

        // 调用代理的方法
        proxyService.m1();
        proxyService.m2();
        proxyService.m3();
    }
//通用代理二：
public void m4() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    // 创建 ServiceA 实例
    ServiceA serviceA = new ServiceA();
    // 1. 创建代理类的处理器
    InvocationHandler invocationHandler = new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            try {
                // 使用反射调用 ServiceA 类的对应方法
                Method serviceAMethod = ServiceA.class.getMethod(method.getName(), method.getParameterTypes());
                return serviceAMethod.invoke(serviceA, args);
            } catch (NoSuchMethodException e) {
                System.out.println("ServiceA 类中没有对应的方法：" + method.getName());
                // 可以执行一些默认逻辑或者抛出异常
                return null;
            }
        }
    };
    // 2. 创建代理实例
    IService proxyService = (IService) Proxy.newProxyInstance(IService.class.getClassLoader(), new Class[]{IService.class}, invocationHandler);
    // 3. 调用代理的方法
    proxyService.m1();
    proxyService.m2();
    proxyService.m3();
}


    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, InstantiationException {
        IService serviceA = CostTimeInvocationHandler.createProxy(new ServiceA(), IService.class);
        IService serviceB = CostTimeInvocationHandler.createProxy(new ServiceB(), IService.class);
        serviceA.m1();
        serviceA.m2();
        serviceA.m3();

        serviceB.m1();
        serviceB.m2();
        serviceB.m3();
    }
}
